package cn.ac.jaas.isc.vegetable.quartz.config;

import cn.ac.jaas.isc.vegetable.quartz.constants.VegetableQuartzEnum;
import cn.ac.jaas.isc.vegetable.quartz.service.SysJobService;
import cn.ac.jaas.isc.vegetable.quartz.util.TaskUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 张高昌
 * @date 2022/4/30 16:09
 * @description: 初始化加载定时任务
 */
@Slf4j
@Configuration
@AllArgsConstructor
public class VegetableInitQuartzJob {

    private final SysJobService sysJobService;

    private final TaskUtil taskUtil;

    private final Scheduler scheduler;

    @Bean
    public void customize() {
        sysJobService.list().forEach(sysJob -> {
            if (VegetableQuartzEnum.JOB_STATUS_RELEASE.getType().equals(sysJob.getJobStatus())) {
                taskUtil.removeJob(sysJob, scheduler);
            } else if (VegetableQuartzEnum.JOB_STATUS_RUNNING.getType().equals(sysJob.getJobStatus())) {
                taskUtil.resumeJob(sysJob, scheduler);
            } else if (VegetableQuartzEnum.JOB_STATUS_NOT_RUNNING.getType().equals(sysJob.getJobStatus())) {
                taskUtil.pauseJob(sysJob, scheduler);
            } else {
                taskUtil.removeJob(sysJob, scheduler);
            }
        });
    }
}
